﻿using System;
using System.IO;
using System.Windows.Input;
using Bulb.Core.Services.CollectionService;
using Cirrious.MvvmCross.Plugins.File;
using Cirrious.MvvmCross.Plugins.PictureChooser;
using Cirrious.MvvmCross.ViewModels;

namespace Bulb.Core.ViewModels
{
    public class BulbViewModel : BaseViewModel
    {
        protected const string ImagePath = "Images";
        protected const string TempPath = "Temp";
        private const int MaxPixelDimension = 400;
        private const int PercentQuality = 95;

        private readonly IMvxPictureChooserTask _pictureChooserTask;
        protected readonly IMvxFileStore FileStore;

        public BulbViewModel(ICollectionService collectionService, IMvxPictureChooserTask pictureChooserTask, IMvxFileStore fileStore)
            : base(collectionService)
        {
            _pictureChooserTask = pictureChooserTask;
            FileStore = fileStore; 
        }

        private byte[] _pictureBytes;
        public byte[] PictureBytes
        {
            get { return _pictureBytes; }
            set { _pictureBytes = value; RaisePropertyChanged(() => PictureBytes); }
        }

        private bool _makeItVisible;
        public bool MakeItVisible
        {
            get { return _makeItVisible; }
            set
            {
                _makeItVisible = value;
                RaisePropertyChanged(() => MakeItVisible);
            }
        }

        private MvxCommand _deletePictureCommand;
        public ICommand DeletePictureCommand
        {
            get
            {
                _deletePictureCommand = _deletePictureCommand ?? new MvxCommand(() =>
                {
                    PictureBytes = null;
                    MakeItVisible = false;
                });
                return _deletePictureCommand;
            }
        }

        private MvxCommand _takePictureCommand;
        public ICommand TakePictureCommand
        {
            get
            {
                _takePictureCommand = _takePictureCommand ?? new MvxCommand(DoTakePicture);
                return _takePictureCommand;
            }
        }

        private void DoTakePicture()
        {
            _pictureChooserTask.TakePicture(MaxPixelDimension, PercentQuality, PictureAvailable, () => { });
        }


        private MvxCommand _choosePictureCommand;
        public ICommand ChoosePictureCommand
        {
            get
            {
                _choosePictureCommand = _choosePictureCommand ?? new MvxCommand(DoChoosePicture);
                return _choosePictureCommand;
            }
        }

        private void DoChoosePicture()
        {
            _pictureChooserTask.ChoosePictureFromLibrary(MaxPixelDimension, PercentQuality, PictureAvailable, () => { });
        }

        protected virtual void PictureAvailable(Stream stream)
        {
            var memoryStream = new MemoryStream();
            stream.CopyTo(memoryStream);
            PictureBytes = memoryStream.ToArray();
            MakeItVisible = true;
        }

        protected string GenerateImagePath(string folderPath)
        {
            if (PictureBytes == null)
                return null;

            var fileName = "Image" + Guid.NewGuid().ToString("N") + ".jpg";
            FileStore.EnsureFolderExists(folderPath);

            var path = FileStore.PathCombine(folderPath, fileName);
            FileStore.WriteFile(path, PictureBytes);

            return path;
        }

        protected void DeleteTemp()
        {
            if (!FileStore.Exists(TempPath)) 
                return;

            foreach (string path in FileStore.GetFilesIn(TempPath))
                FileStore.DeleteFile(path);

            FileStore.DeleteFolder("Temp", false);
        }
    }
}